<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>GateWay | Hexo</title>
  <meta name="keywords" content=" Spring Cloud Alibaba ">
  <meta name="description" content="GateWay | Hexo">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description" content="如何解决消息过期失效问题RabbtiMQ 是可以设置过期时间的，也就是 TTL。如果消息在 queue 中积压超过一定的时间就会被 RabbitMQ 给清理掉，这个数据就没了。这就是说数据不会大量积压在 mq 里，而是大量的数据会直接搞丢。解决方法就是采用批量重导。 批量重导，也就是后期的一种手动补偿手段。当高峰期过去之后将丢失的那部分数据，写个临时程序查出来，重新放到MQ里，把白天的数据补回来。">
<meta property="og:type" content="article">
<meta property="og:title" content="Rabbit MQ（面试）">
<meta property="og:url" content="https://xiaomaer-sss.gitee.io/xiaomaer-sss/2023/02/03/Rabbit%20MQ%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="如何解决消息过期失效问题RabbtiMQ 是可以设置过期时间的，也就是 TTL。如果消息在 queue 中积压超过一定的时间就会被 RabbitMQ 给清理掉，这个数据就没了。这就是说数据不会大量积压在 mq 里，而是大量的数据会直接搞丢。解决方法就是采用批量重导。 批量重导，也就是后期的一种手动补偿手段。当高峰期过去之后将丢失的那部分数据，写个临时程序查出来，重新放到MQ里，把白天的数据补回来。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-02-03T13:04:21.063Z">
<meta property="article:modified_time" content="2023-02-04T03:42:41.460Z">
<meta property="article:author" content="kiwi">
<meta property="article:tag" content="队列">
<meta property="article:tag" content="异步">
<meta name="twitter:card" content="summary">


<link rel="icon" href="/img/avatar.jpg">

<link href="/css/style.css?v=1.1.0" rel="stylesheet">

<link href="/css/hl_theme/atom-light.css?v=1.1.0" rel="stylesheet">

<link href="//cdn.jsdelivr.net/npm/animate.css@4.1.0/animate.min.css" rel="stylesheet">

<script src="//cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="/js/titleTip.js?v=1.1.0" ></script>

<script src="//cdn.jsdelivr.net/npm/highlightjs@9.16.2/highlight.pack.min.js"></script>
<script>
    hljs.initHighlightingOnLoad();
</script>

<script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js"></script>



<script src="//cdn.jsdelivr.net/npm/jquery.cookie@1.4.1/jquery.cookie.min.js" ></script>

<script src="/js/iconfont.js?v=1.1.0" ></script>

<meta name="generator" content="Hexo 5.4.2"></head>
<div style="display: none">
  <input class="theme_disqus_on" value="false">
  <input class="theme_preload_comment" value="">
  <input class="theme_blog_path" value="">
  <input id="theme_shortcut" value="true" />
  <input id="theme_highlight_on" value="true" />
  <input id="theme_code_copy" value="true" />
</div>



<body>
<aside class="nav">
    <div class="nav-left">
        <a href="/"
   class="avatar_target">
    <img class="avatar"
         src="/img/avatar.jpg"/>
</a>
<div class="author">
    <span>kiwi</span>
</div>

<div class="icon">
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
            <a title="csdn"
               href="https://blog.csdn.net/weixin_45340300?type=blog"
               target="_blank">
                
                    <svg class="iconfont-svg" aria-hidden="true">
                        <use xlink:href="#icon-csdn"></use>
                    </svg>
                
            </a>
        
    
        
    
        
    
        
            <a title="email"
               href="mailto:jackma.h@foxmail.com"
               target="_blank">
                
                    <svg class="iconfont-svg" aria-hidden="true">
                        <use xlink:href="#icon-email"></use>
                    </svg>
                
            </a>
        
    
        
            <a title="qq"
               href="http://wpa.qq.com/msgrd?v=3&uin=761791958&site=qq&menu=yes"
               target="_blank">
                
                    <svg class="iconfont-svg" aria-hidden="true">
                        <use xlink:href="#icon-qq"></use>
                    </svg>
                
            </a>
        
    
        
    
        
    
</div>




<ul>
    <li>
        <div class="all active" data-rel="All">All
            
                <small>(34)</small>
            
        </div>
    </li>
    
        
            
                <li>
                    <div data-rel="开发">
                        <i class="fold iconfont icon-right"></i>
                        
                        开发
                        <small>(34)</small>
                        
                    </div>
                    
                        <ul class="sub hide">
                            
                                <li>
                                    <div data-rel="开发<--->框架">
                                        
                                        框架
                                        
                                            <small>(14
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->其他">
                                        
                                        其他
                                        
                                            <small>(5
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->数据库">
                                        
                                        数据库
                                        
                                            <small>(2
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->疑难杂症">
                                        
                                        疑难杂症
                                        
                                            <small>(2
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->中间件">
                                        
                                        中间件
                                        
                                            <small>(5
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->Java">
                                        
                                        Java
                                        
                                            <small>(6
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                        </ul>
                    
                </li>
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
</ul>
<div class="left-bottom">
    <div class="menus">
        
            
            
            
    </div>
    <div>
        
        
    </div>
</div>
<input type="hidden" id="yelog_site_posts_number" value="34">
<input type="hidden" id="yelog_site_word_count" value="83.6k">
<div style="display: none">
    <span id="busuanzi_value_site_uv"></span>
    <span id="busuanzi_value_site_pv"></span>
</div>

    </div>
    <div class="nav-right">
        <div class="friends-area">
    <div class="friends-title">
        Links
        <i class="iconfont icon-left"></i>
    </div>
    <div class="friends-content">
        <ul>
            
            <li><a target="_blank" href="https://xiaomaer-sss.gitee.io/">叶落阁</a></li>
            
        </ul>
    </div>
</div>
        <div class="title-list">
    <div class="right-top">
        <div id="default-panel">
            <i class="iconfont icon-search" data-title="搜索 快捷键 i"></i>
            <div class="right-title">All</div>
            <i class="iconfont icon-file-tree" data-title="切换到大纲视图 快捷键 w"></i>
        </div>
        <div id="search-panel">
            <i class="iconfont icon-left" data-title="返回"></i>
            <input id="local-search-input" autocomplete="off"/>
            <label class="border-line" for="input"></label>
            <i class="iconfont icon-case-sensitive" data-title="大小写敏感"></i>
            <i class="iconfont icon-tag" data-title="标签"></i>
        </div>
        <div id="outline-panel" style="display: none">
            <div class="right-title">大纲</div>
            <i class="iconfont icon-list" data-title="切换到文章列表"></i>
        </div>
    </div>

    <div class="tags-list">
    <input id="tag-search" />
    <div class="tag-wrapper">
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>队列</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>分布式</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>缓存</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>流式处理</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>区块链</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>实战面试题</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>数据库</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>异步</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Bug</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Docker</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java-集合</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java-进阶</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java-JVM</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java基础</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>LB</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>MQ</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Mybatis</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>MySQL</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Redis</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Spring</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Spring Cloud Alibaba</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>SpringBoot</a>
            </li>
        
    </div>

</div>

    
    <nav id="title-list-nav">
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/02/03/Rabbit%20MQ%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="队列,异步"
           data-author="" >
            <span class="post-title" title="Rabbit MQ（面试）">Rabbit MQ（面试）</span>
            <span class="post-date" title="2023-02-03 21:04:21">2023/02/03</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/02/03/%E5%AE%9E%E6%88%98%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="实战面试题"
           data-author="" >
            <span class="post-title" title="实战面试题">实战面试题</span>
            <span class="post-date" title="2023-02-03 17:11:07">2023/02/03</span>
        </a>
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/02/02/Kafka%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="分布式,流式处理,MQ"
           data-author="" >
            <span class="post-title" title="Kafka（面试）">Kafka（面试）</span>
            <span class="post-date" title="2023-02-02 09:51:06">2023/02/02</span>
        </a>
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/02/02/Zookeeper%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="分布式"
           data-author="" >
            <span class="post-title" title="Zookeeper（面试）">Zookeeper（面试）</span>
            <span class="post-date" title="2023-02-02 09:50:33">2023/02/02</span>
        </a>
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/02/01/Redis%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Redis,缓存"
           data-author="" >
            <span class="post-title" title="Redis（面试）">Redis（面试）</span>
            <span class="post-date" title="2023-02-01 14:09:09">2023/02/01</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/30/SpringBoot%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="SpringBoot"
           data-author="" >
            <span class="post-title" title="SpringBoot（面试）">SpringBoot（面试）</span>
            <span class="post-date" title="2023-01-30 20:26:42">2023/01/30</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/30/Mybatis%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Mybatis"
           data-author="" >
            <span class="post-title" title="Mybatis（面试）">Mybatis（面试）</span>
            <span class="post-date" title="2023-01-30 19:13:44">2023/01/30</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/30/Spring%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Spring"
           data-author="" >
            <span class="post-title" title="Spring（面试）">Spring（面试）</span>
            <span class="post-date" title="2023-01-30 19:12:30">2023/01/30</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/29/Java-JVM%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Java-JVM"
           data-author="" >
            <span class="post-title" title="Java-JVM（面试）">Java-JVM（面试）</span>
            <span class="post-date" title="2023-01-29 16:31:34">2023/01/29</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/29/Java-%E8%BF%9B%E9%98%B6%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Java-进阶"
           data-author="" >
            <span class="post-title" title="Java-进阶（面试）">Java-进阶（面试）</span>
            <span class="post-date" title="2023-01-29 13:11:10">2023/01/29</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/29/Java-%E9%9B%86%E5%90%88%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Java-集合"
           data-author="" >
            <span class="post-title" title="Java-集合（面试）">Java-集合（面试）</span>
            <span class="post-date" title="2023-01-29 09:18:51">2023/01/29</span>
        </a>
        
        
        <a  class="All 开发 数据库 "
           href="/xiaomaer-sss/2023/01/28/%E6%95%B0%E6%8D%AE%E5%BA%93-MySQL%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="MySQL"
           data-author="" >
            <span class="post-title" title="数据库-MySQL（面试）">数据库-MySQL（面试）</span>
            <span class="post-date" title="2023-01-28 16:50:59">2023/01/28</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/27/Java-%E5%9F%BA%E7%A1%80%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Java基础"
           data-author="" >
            <span class="post-title" title="Java-基础（面试）">Java-基础（面试）</span>
            <span class="post-date" title="2023-01-27 19:42:58">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/%E5%8C%BA%E5%9D%97%E9%93%BE%E9%92%B1%E5%8C%85/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="区块链钱包">区块链钱包</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/Truffle%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="Truffle常用命令&amp;Web3常用方法">Truffle常用命令&amp;Web3常用方法</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/Truffle&OpenZeppelin%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="Truffle&amp;OpenZeppelin环境搭建">Truffle&amp;OpenZeppelin环境搭建</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/Solidity/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="Solidity">Solidity</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/01/27/Nginx/"
           data-tag="LB"
           data-author="" >
            <span class="post-title" title="Nginx">Nginx</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/27/DockerFile/"
           data-tag="Docker"
           data-author="" >
            <span class="post-title" title="Dockerfile">Dockerfile</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/%E5%8C%BA%E5%9D%97%E9%93%BE%E4%B9%8B%E4%BB%A5%E5%A4%AA%E5%9D%8A/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="区块链之以太坊">区块链之以太坊</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 数据库 "
           href="/xiaomaer-sss/2023/01/27/%E5%B8%B8%E7%94%A8SQL%E8%AF%AD%E6%B3%95%E6%95%B4%E7%90%86/"
           data-tag="MySQL,数据库"
           data-author="" >
            <span class="post-title" title="常用SQL语法整理">常用SQL语法整理</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/27/docker%E8%BF%9B%E9%98%B6/"
           data-tag="Docker"
           data-author="" >
            <span class="post-title" title="Docker进阶">Docker进阶</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/27/Docker%E5%9F%BA%E7%A1%80/"
           data-tag="Docker"
           data-author="" >
            <span class="post-title" title="Docker基础">Docker基础</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/27/Docker%E5%AE%89%E8%A3%85%E5%B8%B8%E8%A7%81%E7%BB%84%E4%BB%B6/"
           data-tag="Docker"
           data-author="" >
            <span class="post-title" title="Docker安装常见组件">Docker安装常见组件</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/SkyWalking/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="SkyWalking">SkyWalking</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/Sentinel/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Sentinel">Sentinel</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/Seata/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Seata">Seata</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/Ribbon/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Ribbon">Ribbon</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 疑难杂症 "
           href="/xiaomaer-sss/2023/01/26/github%E6%8B%89%E5%8F%96%E4%BB%A3%E7%A0%81%E6%8A%A5%20git@github.com's%20password/"
           data-tag="Bug"
           data-author="" >
            <span class="post-title" title="github拉取代码报 &#34;git@github.com&#39;s password&#34;">github拉取代码报 &#34;git@github.com&#39;s password&#34;</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 疑难杂症 "
           href="/xiaomaer-sss/2023/01/26/gitee%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E6%95%99%E7%A8%8B/"
           data-tag="Bug"
           data-author="" >
            <span class="post-title" title="gitee搭建个人博客教程">gitee搭建个人博客教程</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/GateWay/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="GateWay">GateWay</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/Fegin/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Fegin">Fegin</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/20/Nacos/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Nacos">Nacos</span>
            <span class="post-date" title="2023-01-20 17:12:00">2023/01/20</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/17/hello-world/"
           data-tag="Java"
           data-author="" >
            <span class="post-title" title="Hello World">Hello World</span>
            <span class="post-date" title="2023-01-17 15:40:39">2023/01/17</span>
        </a>
        
        <div id="no-item-tips">

        </div>
    </nav>
    <div id="outline-list">
    </div>
</div>

    </div>
    <div class="hide-list">
        <div class="semicircle" data-title="切换全屏 快捷键 s">
            <div class="brackets first"><</div>
            <div class="brackets">&gt;</div>
        </div>
    </div>
</aside>
<div id="post">
    <div class="pjax">
        <article id="post-GateWay" class="article article-type-post" itemscope itemprop="blogPost">
    
        <h1 class="article-title">GateWay</h1>
    
    <div class="article-meta">
        
        
        
        <span class="book">
            <i class="iconfont icon-category"></i>
            
            
            <a  data-rel="开发">开发</a> > 
            
            <a  data-rel="开发&lt;---&gt;框架">框架</a>
            
        </span>
        
        
        <span class="tag">
            <i class="iconfont icon-tag"></i>
            
            <a class="color1">Spring Cloud Alibaba</a>
            
        </span>
        
    </div>
    <div class="article-meta">
        
            Created At : <time class="date" title='Updated At: 2023-01-26 21:57:37'>2023-01-26 21:39</time>
        
    </div>
    <div class="article-meta">
        
        <span>Count:3.1k</span>
        
        
        <span id="busuanzi_container_page_pv">
            Views 👀 :<span id="busuanzi_value_page_pv">
                <span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </span>
        </span>
        
        
    </div>
    
    <div class="toc-ref">
    
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%9F%E8%83%BD%E7%89%B9%E5%BE%81"><span class="toc-text">功能特征</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5"><span class="toc-text">核心概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86"><span class="toc-text">工作原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#GateWay%E5%88%9D%E4%BD%93%E9%AA%8C"><span class="toc-text">GateWay初体验</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#GateWay%E6%95%B4%E5%90%88nacos"><span class="toc-text">GateWay整合nacos</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%AD%E8%A8%80"><span class="toc-text">断言</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E6%96%AD%E8%A8%80%E5%B7%A5%E5%8E%82"><span class="toc-text">内置断言工厂</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%AD%E8%A8%80%E5%B7%A5%E5%8E%82"><span class="toc-text">自定义断言工厂</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-text">过滤器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-text">内置过滤器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-text">自定义过滤器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-text">全局过滤器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Gateway%E8%87%AA%E5%B8%A6%E5%86%85%E7%BD%AE%E5%85%A8%E5%B1%80%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-text">Gateway自带内置全局过滤器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%85%A8%E5%B1%80%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-text">自定义全局过滤器</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Gateway%E6%95%B4%E5%90%88sentinel"><span class="toc-text">Gateway整合sentinel</span></a></li></ol>
    
<style>
    .left-col .switch-btn,
    .left-col .switch-area {
        display: none;
    }
    .toc-level-3 i,
    .toc-level-3 ol {
        display: none !important;
    }
</style>
</div>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>API网关，就是指系统的统一入口，它封装了应用程序的内部结构，为客户端提供统一服务，一些与业务本身功能无关的公共逻辑可以在这里实现，诸如认证、鉴权、监控、路由转发等等。 </p>
<p>官方文档地址：<a target="_blank" rel="noopener" href="https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/">https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/</a></p>
<p>API网关可体统的功能举例：</p>
<ul>
<li><strong>关注稳定与安全</strong><ul>
<li>全局性流控</li>
<li>日志统计</li>
<li>防止SQL注入</li>
<li>防止Web攻击</li>
<li>屏蔽工具扫描</li>
<li>黑白IP名单</li>
<li>证书/加解密处理</li>
</ul>
</li>
<li><strong>提供更好的服务</strong><ul>
<li>服务级别流控</li>
<li>服务降级与熔断</li>
<li>路由与负载均衡、灰度策略</li>
<li>服务过滤、聚合与发现</li>
<li>权限验证与用户级别策略</li>
<li>业务规则与参数校验</li>
<li>多级缓存策略</li>
</ul>
</li>
</ul>
<p>Spring Cloud Gateway 是Spring Cloud官方推出的第二代网关框架，定位于取代 Netflix Zuul1.0。相比 Zuul 来说，Spring Cloud Gateway 提供更优秀的性能，更强大的有功能。<br>Spring Cloud Gateway 是由 WebFlux + Netty + Reactor 实现的响应式的 API 网关。<strong>它不能在传统的 servlet 容器中工作，也不能构 建成 war 包。</strong><br>Spring Cloud Gateway 旨在为微服务架构提供一种简单且有效的 API 路由的管理方式，并基于 Filter 的方式提供网关的基本功能，例如 说安全认证、监控、限流等等。 </p>
<h2 id="功能特征"><a href="#功能特征" class="headerlink" title="功能特征"></a>功能特征</h2><ul>
<li>基于Spring Framework 5, Project Reactor 和 Spring Boot 2.0 进行构建</li>
<li>动态路由：能够匹配任何请求属性</li>
<li>支持路径重写</li>
<li>集成 Spring Cloud 服务发现功能（Nacos、Eruka）</li>
<li>可集成流控降级功能（Sentinel、Hystrix）</li>
<li>可以对路由指定易于编写的 Predicate（断言）和 Filter（过滤器）</li>
</ul>
<h2 id="核心概念"><a href="#核心概念" class="headerlink" title="核心概念"></a>核心概念</h2><ul>
<li><strong>路由</strong>（route) 路由是网关中最基础的部分，路由信息包括一个ID、一个目的URI、一组断言工厂、一组Filter组成。如果断言为真，则说明请求的URL和 配置的路由匹配。</li>
<li><strong>断言</strong>(predicates) Java8中的断言函数，SpringCloud Gateway中的断言函数类型是Spring5.0框架中的ServerWebExchange。断言函数允许开发者去定义匹配Http request中的任何信息，比如请求头和参数等。 </li>
<li><strong>过滤器</strong>（Filter) SpringCloud Gateway中的filter分为Gateway FilIer和Global Filter。Filter可以对请求和响应进行处理。 </li>
</ul>
<h2 id="工作原理"><a href="#工作原理" class="headerlink" title="工作原理"></a>工作原理</h2><p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1672492349406.png" alt="1672537026645"></p>
<p><strong>执行流程如下：</strong></p>
<ol>
<li>Gateway Client向Gateway Server发送请求 。</li>
<li>请求首先会被HttpWebHandlerAdapter进行提取组装成网关上下文 。</li>
<li>然后网关的上下文会传递到DispatcherHandler，它负责将请求分发给RoutePredicateHandlerMapping 。</li>
<li>RoutePredicateHandlerMapping负责路由查找，并根据路由断言判断路由是否可用 。</li>
<li>如果过断言成功，由FilteringWebHandler创建过滤器链并调用 。</li>
<li>请求会一次经过PreFilter–微服务–PostFilter的方法，最终返回响应 。</li>
</ol>
<h2 id="GateWay初体验"><a href="#GateWay初体验" class="headerlink" title="GateWay初体验"></a>GateWay初体验</h2><ul>
<li><p>构建一个SpringBoot项目</p>
</li>
<li><p>导入依赖</p>
<pre><code class="pom">#父工程中已经引入的 spring cloud 其他的依赖
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-gateway&lt;/artifactId&gt;
    &lt;exclusions&gt;
        &lt;exclusion&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/exclusion&gt;
    &lt;/exclusions&gt;
&lt;/dependency&gt;
</code></pre>
</li>
<li><p>添加配置文件</p>
<pre><code class="yml">server:
  port: 8088
spring:
  application:
    name: api-gateway
  cloud:
    # gateway的配置
    gateway:
      # 路由规则
      routes:
        - id: order_route   # 路由的唯一标识，路由到order
          uri: http://localhost:8010   # 需要转发的地址
          # 断言规则 用于路由规则的匹配
          predicates:
            - Path=/order-serv/**
              # http://localhost:8088/order-serv/order/add  路由到 ↓
              # http://localhost:8010/order-serv/order/add
          filters:
            - StripPrefix=1  # 转发之前去掉一级路径 order-serv
            # http://localhost:8010/order/add
</code></pre>
</li>
<li><p>通过GateWay访问配置的规则<img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1672548934692.png" alt="1672548934692"></p>
</li>
</ul>
<h2 id="GateWay整合nacos"><a href="#GateWay整合nacos" class="headerlink" title="GateWay整合nacos"></a>GateWay整合nacos</h2><ul>
<li><p>添加依赖</p>
<pre><code class="pom">&lt;dependency&gt;
    &lt;groupId&gt;com.alibaba.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-alibaba-nacos-discovery&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
</li>
<li><p>修改配置文件</p>
<pre><code class="yml">server:
  port: 8088
spring:
  application:
    name: api-gateway
  cloud:
    # gateway的配置
    gateway:
      # 路由规则
      routes:
        - id: order_route   # 路由的唯一标识，路由到order
          uri: lb://order-service  # 需要转发的地址  # lb是从nacos中按照名称获取服务，并遵循负载均衡策略
          # 断言规则 用于路由规则的匹配
          predicates:
            - Path=/order-serv/**
              # http://localhost:8088/order-serv/order/add  路由到 ↓
              # http://localhost:8010/order-serv/order/add
          filters:
            - StripPrefix=1  # 转发之前去掉一级路径 order-serv
            # http://localhost:8010/order/add
    # 添加nacos配置信息
    nacos:
      server-addr: 192.168.2.7:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
</code></pre>
</li>
</ul>
<h2 id="断言"><a href="#断言" class="headerlink" title="断言"></a>断言</h2><p>当请求gateway的时候， 使用断言对请求进行匹配， 如果匹配成功就路由转发， 如果匹配失败就返回404 。</p>
<h3 id="内置断言工厂"><a href="#内置断言工厂" class="headerlink" title="内置断言工厂"></a>内置断言工厂</h3><ul>
<li><p><strong>基于Datetime类型的断言工厂</strong><br>此类型的断言根据时间做判断，主要有三个：</p>
<ol>
<li><p>AfterRoutePredicateFactory： 接收一个日期参数，判断请求日期是否晚于指定日期</p>
<pre><code class="yml">- After=2017-01-20T17:42:47.789-07:00[America/Denver]
</code></pre>
</li>
<li><p>BeforeRoutePredicateFactory： 接收一个日期参数，判断请求日期是否早于指定日期</p>
<pre><code class="yml">- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
</code></pre>
</li>
<li><p>BetweenRoutePredicateFactory： 接收两个日期参数，判断请求日期是否在指定时间段内</p>
<pre><code class="yml">- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
</code></pre>
</li>
</ol>
</li>
<li><p><strong>基于远程地址的断言工厂</strong><br>RemoteAddrRoutePredicateFactory：接收一个IP地址段，判断请求主机地址是否在地址段中</p>
<pre><code class="yml">‐ RemoteAddr=192.168.1.1/24
</code></pre>
</li>
<li><p><strong>基于Cookie的断言工厂</strong><br>CookieRoutePredicateFactory：接收两个参数，cookie 名字和一个正则表达式。 判断请求 。 Cookie是否具有给定名称且值与正则表达式匹配。</p>
<pre><code class="yml"> ‐Cookie=chocolate, ch.
</code></pre>
</li>
<li><p><strong>基于Header的断言工厂</strong><br>HeaderRoutePredicateFactory：接收两个参数，标题名称和正则表达式。 判断请求Header是否具有给定名称且值与正则表达式匹配。 </p>
<pre><code class="yml"> ‐Header=X‐Request‐Id, \d+
</code></pre>
</li>
<li><p><strong>基于Host的断言工厂</strong><br>HostRoutePredicateFactory：接收一个参数，主机名模式。判断请求的Host是否满足匹配规则。 </p>
<pre><code class="yml"> ‐Host=**.testhost.org
</code></pre>
</li>
<li><p><strong>基于Method请求方法的断言工厂</strong><br>MethodRoutePredicateFactory：接收一个参数，判断请求类型是否跟指定的类型匹配。 </p>
<pre><code class="yml"> ‐Method=GET
</code></pre>
</li>
<li><p><strong>基于Path请求路径的断言工厂</strong><br>PathRoutePredicateFactory：接收一个参数，判断请求的URI部分是否满足路径规则。 </p>
<pre><code class="yml"> ‐Path=/foo/&#123;segment&#125;
</code></pre>
</li>
<li><p><strong>基于Query请求参数的断言工厂</strong><br>QueryRoutePredicateFactory ：接收两个参数，请求param和正则表达式， 判断请求参数是否具有给定名称且值与正则表达式匹配。</p>
<pre><code class="yml"> ‐Query=baz, ba.
</code></pre>
</li>
<li><p><strong>基于路由权重的断言工厂</strong><br>WeightRoutePredicateFactory：接收一个[组名,权重], 然后对于同一个组内的路由按照权重转发。</p>
<pre><code> routes:
 ‐id: weight_route1
  uri: host1
  predicates:
 ‐Path=/product/**
 ‐Weight=group3, 1
 ‐id: weight_route2
  uri: host2
  predicates:
 ‐Path=/product/**
 ‐Weight= group3, 9
</code></pre>
</li>
</ul>
<h3 id="自定义断言工厂"><a href="#自定义断言工厂" class="headerlink" title="自定义断言工厂"></a>自定义断言工厂</h3><ol>
<li>自定义断言工厂必须交个spring管理</li>
<li><strong>自定义断言工厂类名必须以RoutePredicateFactory结尾</strong>（约定大于配置的思想）</li>
<li>继承AbstractRoutePredicateFactory抽象类</li>
<li>自定义断言工厂内部声明静态内部类，声明属性用来接收配置文件中对应的断言信息</li>
<li>使用shortcutFieldOrder方法进行数据绑定</li>
<li>在applay内进行逻辑判断</li>
</ol>
<pre><code class="java">@Component
public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory&lt;MyRoutePredicateFactory.Config&gt; &#123;


    public MyRoutePredicateFactory() &#123;
        super(Config.class);
    &#125;

    public Predicate&lt;ServerWebExchange&gt; apply(Config config) &#123;
        return new GatewayPredicate() &#123;
            public boolean test(ServerWebExchange serverWebExchange) &#123;
                return config.getName().equals(&quot;zhangsan&quot;);
            &#125;

            public String toString() &#123;
                return String.format(&quot;MyRoute: param=%s&quot;, config.getName());
            &#125;
        &#125;;
    &#125;

    public List&lt;String&gt; shortcutFieldOrder() &#123;
        return Collections.singletonList(&quot;name&quot;);
    &#125;

    public static class Config&#123;
        private String name;

        public String getName() &#123;
            return name;
        &#125;

        public void setName(String name) &#123;
            this.name = name;
        &#125;
    &#125;
&#125;
</code></pre>
<h2 id="过滤器"><a href="#过滤器" class="headerlink" title="过滤器"></a>过滤器</h2><h3 id="内置过滤器"><a href="#内置过滤器" class="headerlink" title="内置过滤器"></a>内置过滤器</h3><p>Gateway 内置了很多的过滤器工厂，我们通过一些过滤器工厂可以进行一些业务逻辑处理器，比如添加剔除响应头，添加去除参数等。</p>
<p>官网中的过滤器都有示例，可参考官网</p>
<p><a target="_blank" rel="noopener" href="https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/#gatewayfilter-factories">https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/#gatewayfilter-factories</a></p>
<table>
<thead>
<tr>
<th>过滤器工厂</th>
<th>作用</th>
<th>参数</th>
</tr>
</thead>
<tbody><tr>
<td>AddRequestHeader</td>
<td>为原始请求添加Header</td>
<td>Header的名称及值</td>
</tr>
<tr>
<td>AddRequestParameter</td>
<td>为原始请求添加请求参数</td>
<td>参数名称及值</td>
</tr>
<tr>
<td>AddResponseHeader</td>
<td>为原始响应添加Header</td>
<td>Header的名称及值</td>
</tr>
<tr>
<td>DedupeResponseHeader</td>
<td>剔除响应头中重复的值</td>
<td>需要去重的Header名称及去重策略</td>
</tr>
<tr>
<td>Hystrix</td>
<td>为路由引入Hystrix的断路器保护</td>
<td>HystrixCommand 的名称</td>
</tr>
<tr>
<td>FallbackHeaders</td>
<td>为fallbackUri的请求头中添加具体的异常信息</td>
<td>Header的名称</td>
</tr>
<tr>
<td>PreﬁxPath</td>
<td>为原始请求路径添加前缀</td>
<td>前缀路径</td>
</tr>
<tr>
<td>PreserveHostHeader</td>
<td>为请求添加一个preserveHostHeader=true 的 属性，路由过滤器会检查该属性以决定是否要发送原始的Host</td>
<td>无</td>
</tr>
<tr>
<td>RequestRateLimiter</td>
<td>用于对请求限流，限流算法为令牌桶</td>
<td>keyResolver、rateLimiter、statusCode、denyEmptyKey、emptyKeyStatus</td>
</tr>
<tr>
<td>RedirectTo</td>
<td>将原始请求重定向到指定的URL</td>
<td>http状态码及重定向的url</td>
</tr>
<tr>
<td>RemoveHopByHopHeadersFilter</td>
<td>为原始请求删除IETF组织规定的一系列Header</td>
<td>默认就会启用，可以通过配置指定仅删除哪些Header</td>
</tr>
<tr>
<td>RemoveRequestHeader</td>
<td>为原始请求删除某个Header</td>
<td>Header名称</td>
</tr>
<tr>
<td>RemoveResponseHeader</td>
<td>为原始响应删除某个Header</td>
<td>Header名称</td>
</tr>
<tr>
<td>RewritePath</td>
<td>重写原始的请求路径</td>
<td>原始路径正则表达式以及重写后路径的正则表达式</td>
</tr>
<tr>
<td>RewriteResponseHeader</td>
<td>重写原始响应中的某个Header</td>
<td>Header名称，值的正 则表达式，重写后的值</td>
</tr>
<tr>
<td>SaveSession</td>
<td>在转发请求之前，强制执行WebSession::save 操作</td>
<td>无</td>
</tr>
<tr>
<td>SecureHeaders</td>
<td>为原始响应添加一系列起安全作用的响应头</td>
<td>无，支持修改这些安全响应头的值</td>
</tr>
<tr>
<td>SetPath</td>
<td>修改原始的请求路径</td>
<td>修改后的路径</td>
</tr>
<tr>
<td>SetResponseHeader</td>
<td>修改原始响应中某个Header的值</td>
<td>Header名称，修改后的值</td>
</tr>
<tr>
<td>SetStatus</td>
<td>修改原始响应的状态码</td>
<td>HTTP 状态码，可以是数字，也可以是字符串</td>
</tr>
<tr>
<td>StripPreﬁx</td>
<td>用于截断原始请求的路径</td>
<td>使用数字表示要截断的路径的数量</td>
</tr>
<tr>
<td>Retry</td>
<td>针对不同的响应进行重试</td>
<td>retries、statuses、methods、series</td>
</tr>
<tr>
<td>RequestSize</td>
<td>设置允许接收最大请求包的大小。如果请求包大小超过设置的值，则返回 413 Payload Too Large</td>
<td>请求包大小，单位为字节，默认值为5M</td>
</tr>
<tr>
<td>ModifyRequestBody</td>
<td>在转发请求之前修改原始请求体内容</td>
<td>修改后的请求体内容</td>
</tr>
<tr>
<td>ModifyResponseBody</td>
<td>修改原始响应体的内容</td>
<td>修改后的响应体内容</td>
</tr>
</tbody></table>
<h3 id="自定义过滤器"><a href="#自定义过滤器" class="headerlink" title="自定义过滤器"></a>自定义过滤器</h3><p>自定义的过滤器和前面的自定义断言工厂流程差不多。</p>
<p>自定义过滤器对响应头进行了添加，MyGatewayFilter：test filter</p>
<p>代码如下：</p>
<pre><code class="java">@Component
public class MyGatewayFilterFactory extends AbstractGatewayFilterFactory&lt;MyGatewayFilterFactory.MyConfig&gt; &#123;

    public MyGatewayFilterFactory() &#123;
        super(MyGatewayFilterFactory.MyConfig.class);
    &#125;

    @Override
    public GatewayFilter apply(MyConfig config) &#123;
        return new GatewayFilter() &#123;
            @Override
            public Mono&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) &#123;
                String name = exchange.getRequest().getQueryParams().getFirst(&quot;name&quot;);
                if(StringUtils.isNotBlank(name))&#123;
                    if (config.getValue().equals(name))&#123;
                        exchange.getResponse().getHeaders().set(&quot;MyGatewayFilter&quot;,&quot;test filter&quot;);
                        return chain.filter(exchange);
                    &#125;else&#123;
                        exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
                        exchange.getResponse().setComplete();
                    &#125;
                &#125;
                //正常请求
                return chain.filter(exchange);
            &#125;
        &#125;;
    &#125;

    @Override
    public List&lt;String&gt; shortcutFieldOrder() &#123;
        return Arrays.asList(&quot;value&quot;,&quot;key&quot;);
    &#125;

    public static class MyConfig&#123;
        private String value;
        private String key;

        public String getValue() &#123;
            return value;
        &#125;

        public void setValue(String value) &#123;
            this.value = value;
        &#125;

        public String getKey() &#123;
            return key;
        &#125;

        public void setKey(String key) &#123;
            this.key = key;
        &#125;
    &#125;
&#125;
</code></pre>
<p>配置文件如下：</p>
<pre><code class="yml">server:
  port: 8088
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: order_route
          uri: lb://order-service
          predicates:
            - Path=/order-serv/**
            - My=zhangsan
          filters:
            - My=zhangsan
            - StripPrefix=1
    nacos:
      server-addr: 192.168.2.7:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
</code></pre>
<p>查看效果：<br><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1672715698793.png" alt="1672715698793"></p>
<h3 id="全局过滤器"><a href="#全局过滤器" class="headerlink" title="全局过滤器"></a>全局过滤器</h3><p><strong>局部过滤器和全局过滤器的区别：</strong></p>
<p>GlobalFilter接口和GatewayFilter 有一样的接口定义，但GlobalFilter 会作用于所有路由。 </p>
<p>局部：针对某个路由，需要在所应用的路由中进行配置。</p>
<p>全局：针对所有路由请求，一旦定义就可以生效。</p>
<h4 id="Gateway自带内置全局过滤器"><a href="#Gateway自带内置全局过滤器" class="headerlink" title="Gateway自带内置全局过滤器"></a>Gateway自带内置全局过滤器</h4><p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1672723480609.png" alt="1672723480609"></p>
<h4 id="自定义全局过滤器"><a href="#自定义全局过滤器" class="headerlink" title="自定义全局过滤器"></a>自定义全局过滤器</h4><p>实现GlobalFilter接口即可；</p>
<pre><code class="java">@Component
public class MyGlobalLogFilter implements GlobalFilter &#123;

    Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public Mono&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) &#123;
        log.info(&quot;访问路径为&#123;&#125;&quot;,exchange.getRequest().getPath().value());
        return chain.filter(exchange);
    &#125;
&#125;
</code></pre>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1672725259843.png" alt="1672725259843"></p>
<h2 id="Gateway整合sentinel"><a href="#Gateway整合sentinel" class="headerlink" title="Gateway整合sentinel"></a>Gateway整合sentinel</h2><p>网关限流官方文档：</p>
<p><a target="_blank" rel="noopener" href="https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81">https://github.com/alibaba/Sentinel/wiki/网关限流</a></p>
<ul>
<li><p>添加依赖</p>
<pre><code class="pom">&lt;dependency&gt;
    &lt;groupId&gt;com.alibaba.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-alibaba-sentinel&lt;/artifactId&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
    &lt;groupId&gt;com.alibaba.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-alibaba-sentinel-gateway&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
</li>
<li><p>添加配置文件</p>
<pre><code class="yml">    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
</code></pre>
</li>
<li><p>启动项目、nacos、sentinel</p>
</li>
<li><p>查看效果</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1672797788975.png" alt="1672797788975"></p>
</li>
</ul>
<p>这个控制台和单纯的项目整合sentinel的页面有一些不一样的地方，大配置基本大同小异。一些流控规则可以参考我<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_45340300/article/details/128660469?spm=1001.2014.3001.5501">sentinel</a>笔记</p>

      
       <hr><span style="font-style: italic;color: gray;"> 转载请注明来源，欢迎对文章中的引用来源进行考证，欢迎指出任何有错误或不够清晰的表达。可以邮件至 jackma.h@foxmail.com </span>
    </div>
</article>


<p>
    <a  class="dashang" onclick="dashangToggle()">💰</a>
</p>






    




    </div>
    <div class="copyright">
        <p class="footer-entry">
    ©2017 kiwi
</p>
<p class="footer-entry">Built with <a href="https://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/yelog/hexo-theme-3-hexo" target="_blank">3-hexo</a> theme</p>

    </div>
    <div class="full-toc">
        <button class="full" data-title="切换全屏 快捷键 s"><span class="min "></span></button>
<a class="" id="rocket" ></a>

    </div>
</div>

<div class="hide_box" onclick="dashangToggle()"></div>
<div class="shang_box">
    <a class="shang_close"  onclick="dashangToggle()">×</a>
    <div class="shang_tit">
        <p>Help us with donation</p>
    </div>
    <div class="shang_payimg">
        <div class="pay_img">
            <img src="/img/alipay.jpg" class="alipay" title="扫码支持">
            <img src="/img/weixin.jpg" class="weixin" title="扫码支持">
        </div>
    </div>
    <div class="shang_payselect">
        <span><label><input type="radio" name="pay" checked value="alipay">alipay</label></span><span><label><input type="radio" name="pay" value="weixin">weixin</label></span>
    </div>
</div>


</body>
<script src="/js/jquery.pjax.js?v=1.1.0" ></script>

<script src="/js/script.js?v=1.1.0" ></script>
<script>
    var img_resize = 'default';
    function initArticle() {
        /*渲染对应的表格样式*/
        
            $("#post .pjax table").addClass("green_title");
        

        /*渲染打赏样式*/
        
        $("input[name=pay]").on("click", function () {
            if($("input[name=pay]:checked").val()=="weixin"){
                $(".shang_box .shang_payimg .pay_img").addClass("weixin_img");
            } else {
                $(".shang_box .shang_payimg .pay_img").removeClass("weixin_img");
            }
        })
        

        /*高亮代码块行号*/
        

        /*访问数量*/
        
        $.getScript("//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js");
        

        /*代码高亮，行号对齐*/
        $('.pre-numbering').css('line-height',$('.has-numbering').css('line-height'));

        
        
    }

    /*打赏页面隐藏与展示*/
    
    function dashangToggle() {
        $(".shang_box").fadeToggle();
        $(".hide_box").fadeToggle();
    }
    

</script>

<!--加入行号的高亮代码块样式-->

<!--自定义样式设置-->
<style>
    
    
    .nav {
        width: 542px;
    }
    .nav.fullscreen {
        margin-left: -542px;
    }
    .nav-left {
        width: 120px;
    }
    
    
    @media screen and (max-width: 1468px) {
        .nav {
            width: 492px;
        }
        .nav.fullscreen {
            margin-left: -492px;
        }
        .nav-left {
            width: 100px;
        }
    }
    
    
    @media screen and (max-width: 1024px) {
        .nav {
            width: 492px;
            margin-left: -492px
        }
        .nav.fullscreen {
            margin-left: 0;
        }
    }
    
    @media screen and (max-width: 426px) {
        .nav {
            width: 100%;
        }
        .nav-left {
            width: 100%;
        }
    }
    
    
    .nav-right .title-list nav a .post-title, .nav-right .title-list #local-search-result a .post-title {
        color: #383636;
    }
    
    
    .nav-right .title-list nav a .post-date, .nav-right .title-list #local-search-result a .post-date {
        color: #5e5e5f;
    }
    
    
    .nav-right nav a.hover, #local-search-result a.hover{
        background-color: #e2e0e0;
    }
    
    

    /*列表样式*/
    
    #post .pjax article .article-entry>ol, #post .pjax article .article-entry>ul, #post .pjax article>ol, #post .pjax article>ul{
        border: #e2dede solid 1px;
        border-radius: 10px;
        padding: 10px 32px 10px 56px;
    }
    #post .pjax article .article-entry li>ol, #post .pjax article .article-entry li>ul,#post .pjax article li>ol, #post .pjax article li>ul{
        padding-top: 5px;
        padding-bottom: 5px;
    }
    #post .pjax article .article-entry>ol>li, #post .pjax article .article-entry>ul>li,#post .pjax article>ol>li, #post .pjax article>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    #post .pjax article .article-entry li>ol>li, #post .pjax article .article-entry li>ul>li,#post .pjax article li>ol>li, #post .pjax article li>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    

    /* 背景图样式 */
    
    #post {
        background: url([object Object]);
    }
    
    
    #post .index {
        background: url([object Object]);
    }
    


    /*引用块样式*/
    

    /*文章列表背景图*/
    
    .nav-right:before {
        content: ' ';
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        opacity: 0.3;
        background: url("https://i.loli.net/2019/07/22/5d3521411f3f169375.png");
        background-repeat: no-repeat;
        background-position: 50% 0;
        -ms-background-size: cover;
        -o-background-size: cover;
        -moz-background-size: cover;
        -webkit-background-size: cover;
        background-size: cover;
    }
    

    
</style>







</html>
